package com.bdth.weget.business.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.bdth.weget.business.entity.Cars;
import com.bdth.weget.business.entity.Location;
import com.bdth.weget.business.mapper.LocationMapper;
import com.bdth.weget.business.service.CarsService;
import com.bdth.weget.business.service.LocationService;
import com.bdth.weget.util.DateUtil;
import com.bdth.weget.vo.JT0200;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2019/7/15.
 */
@Service
public class LocationServiceImpl extends ServiceImpl<LocationMapper, Location> implements LocationService {
    @Autowired
    private LocationMapper locationMapper;

    @Override
    public boolean saveLocation(JT0200 bean) {
        Location location = findBySimno(bean.getSimno());
        if(location!=null){
            //BeanUtils.copyProperties(bean.getLocation(),location);
            location.setSimno(bean.getSimno());
            location.setCarid(bean.getCarid());
            location.setLatitude(bean.getLocation().getLatitude());
            location.setLongitude(bean.getLocation().getLongitude());
            location.setSpeed(bean.getLocation().getSpeed());
            location.setUpdatetime(DateUtil.str2date(bean.getTime(),"yyyy-MM-dd HH:mm:ss"));
            location.setAltitude((int)bean.getLocation().getAltitude());
            location.setCourse((int)bean.getLocation().getCourse());
            locationMapper.updateById(location);
        }else{
            //BeanUtils.copyProperties(bean.getLocation(),location);
            location = new Location();
            location.setSimno(bean.getSimno());
            location.setCarid(bean.getCarid());
            location.setLatitude(bean.getLocation().getLatitude());
            location.setLongitude(bean.getLocation().getLongitude());
            location.setSpeed(bean.getLocation().getSpeed());
            location.setAltitude((int)bean.getLocation().getAltitude());
            location.setCourse((int)bean.getLocation().getCourse());
            location.setUpdatetime(DateUtil.str2date(bean.getTime(),"yyyy-MM-dd HH:mm:ss"));
            locationMapper.insert(location);
        }
        return true;
    }

    @Override
    public Location findBySimno(String simno) {
        EntityWrapper<Location> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("simno",simno);
        List<Location> list = locationMapper.selectList(entityWrapper);
        if(list!=null && list.size()>0)
            return list.get(0);
        return null;
    }


}
